﻿<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools Documentation - Types/String</title>
</head>

<body>
  <div id="docs" class="doc">
    <div class="span-2 last methods">
      <h4 class="menu-item"><a href="#String">String</a></h4>
      <div class="menu-item"><a href="#String:String-from">String.from</a></div>
      <div class="menu-item"><a href="#String:String-uniqueID">String.uniqueID</a></div>
      <div class="menu-item"><a href="#String:test">test</a></div>
      <div class="menu-item"><a href="#String:contains">contains</a></div>
      <div class="menu-item"><a href="#String:trim">trim</a></div>
      <div class="menu-item"><a href="#String:clean">clean</a></div>
      <div class="menu-item"><a href="#String:camelCase">camelCase</a></div>
      <div class="menu-item"><a href="#String:hyphenate">hyphenate</a></div>
      <div class="menu-item"><a href="#String:capitalize">capitalize</a></div>
      <div class="menu-item"><a href="#String:escapeRegExp">escapeRegExp</a></div>
      <div class="menu-item"><a href="#String:toInt">toInt</a></div>
      <div class="menu-item"><a href="#String:toFloat">toFloat</a></div>
      <div class="menu-item"><a href="#String:hexToRgb">hexToRgb</a></div>
      <div class="menu-item"><a href="#String:rgbToHex">rgbToHex</a></div>
      <div class="menu-item"><a href="#String:substitute">substitute</a></div>
      <div class="menu-item"><a href="#String:stripScripts">stripScripts</a></div>
    </div>
    <div class="l-border r-border span-15 content">
				<h1 id="String"><a href="#String">Type: String</a></h1>

<p class="description">A collection of the String Object methods and functions.</p>

<h3>See Also:</h3>

<ul>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String">MDC String</a></li>
</ul>
<h2 id="String:String-from"><a href="#String:String-from">Function: String.from</a></h2>

<p class="description">Returns the passed parameter as a String.</p>

<h3>Syntax:</h3>

<pre class="javascript">String.<span class="me1">from</span><span class="br0">(</span>arg<span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>arg - (<em>mixed</em>) The argument to return as a string.</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The argument as a string.</li>
</ul>
<h3>Example:</h3>

<pre class="javascript">String.<span class="me1">from</span><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span>; <span class="co1">// returns '2'</span>
String.<span class="me1">from</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// returns 'true'</span>
</pre>

<h2 id="String:String-uniqueID"><a href="#String:String-uniqueID">Function: String.uniqueID</a></h2>

<p class="description">Generates a unique ID</p>

<h3>Syntax:</h3>

<pre class="javascript">String.<span class="me1">uniqueID</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) A unique ID.</li>
</ul>
<h3>Example:</h3>

<pre class="javascript">String.<span class="me1">uniqueID</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h2 id="String:test"><a href="#String:test">String method: test</a></h2>

<p class="description">Searches for a match between the string and a regular expression.
For more information see <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp/test">MDC Regexp:test</a>.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">test</span><span class="br0">(</span>regex<span class="br0">[</span>, params<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>regex  - (<em>mixed</em>) The string or regular expression you want to match the string with.</li>
<li>params - (<em>string</em>, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>boolean</em>) <code>true</code>, if a match for the regular expression is found in this string.</li>
<li>(<em>boolean</em>) <code>false</code> if is not found</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'cookie'</span><span class="br0">)</span>; <span class="co1">// returns true</span>
<span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'COOKIE'</span>, <span class="st0">'i'</span><span class="br0">)</span>; <span class="co1">// returns true (ignore case)</span>
<span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'cake'</span><span class="br0">)</span>; <span class="co1">// returns false</span>
</pre>

<h3>See Also:</h3>

<ul>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions">MDC Regular Expressions</a></li>
</ul>
<h2 id="String:contains"><a href="#String:contains">String method: contains</a></h2>

<p class="description">Checks to see if the string passed in is contained in this string.
If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">contains</span><span class="br0">(</span>string<span class="br0">[</span>, separator<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>string    - (<em>string</em>) The string to search for.</li>
<li>separator - (<em>string</em>, optional) The string that separates the values in this string (e.g. Element classNames are separated by a ' ').</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>boolean</em>) <code>true</code> if the string is contained in this string</li>
<li>(<em>boolean</em>) <code>false</code> if not.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'a bc'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'bc'</span><span class="br0">)</span>; <span class="co1">// returns true</span>
<span class="st0">'a b c'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'c'</span>, <span class="st0">' '</span><span class="br0">)</span>; <span class="co1">// returns true</span>
<span class="st0">'a bc'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'b'</span>, <span class="st0">' '</span><span class="br0">)</span>; <span class="co1">// returns false</span>
</pre>

<h2 id="String:trim"><a href="#String:trim">String method: trim</a></h2>

<p class="description">Trims the leading and trailing spaces off a string.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">trim</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The trimmed string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'    i like cookies     '</span>.<span class="me1">trim</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'i like cookies'</span>
</pre>

<h2 id="String:clean"><a href="#String:clean">String method: clean</a></h2>

<p class="description">Removes all extraneous whitespace from a string and trims it (<a href="#String:trim">String:trim</a>).</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">clean</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The cleaned string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">' i      like     cookies      <span class="es0">\n</span><span class="es0">\n</span>'</span>.<span class="me1">clean</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'i like cookies'</span>
</pre>

<h2 id="String:camelCase"><a href="#String:camelCase">String method: camelCase</a></h2>

<p class="description">Converts a hyphenated string to a camelcased string.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">camelCase</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The camelcased string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'I-like-cookies'</span>.<span class="me1">camelCase</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'ILikeCookies'</span>
</pre>

<h2 id="String:hyphenate"><a href="#String:hyphenate">String method: hyphenate</a></h2>

<p class="description">Converts a camelcased string to a hyphenated string.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">hyphenate</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The hyphenated string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'ILikeCookies'</span>.<span class="me1">hyphenate</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns '-i-like-cookies'</span>
</pre>

<h2 id="String:capitalize"><a href="#String:capitalize">String method: capitalize</a></h2>

<p class="description">Converts the first letter of each word in a string to uppercase.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">capitalize</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The capitalized string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'i like cookies'</span>.<span class="me1">capitalize</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'I Like Cookies'</span>
</pre>

<h2 id="String:escapeRegExp"><a href="#String:escapeRegExp">String method: escapeRegExp</a></h2>

<p class="description">Escapes all regular expression characters from the string.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">escapeRegExp</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) The escaped string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'animals.sheep[1]'</span>.<span class="me1">escapeRegExp</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'animals\.sheep\[1\]'</span>
</pre>

<h2 id="String:toInt"><a href="#String:toInt">String method: toInt</a></h2>

<p class="description">Parses this string and returns a number of the specified radix or base.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">[</span>base<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>base - (<em>number</em>, optional) The base to use (defaults to 10).</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>number</em>) The number.</li>
<li>(<em>NaN</em>) If the string is not numeric, returns NaN.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'4em'</span>.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 4</span>
<span class="st0">'10px'</span>.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 10</span>
</pre>

<h3>See Also:</h3>

<ul>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt">MDC parseInt</a></li>
</ul>
<h2 id="String:toFloat"><a href="#String:toFloat">String method: toFloat</a></h2>

<p class="description">Parses this string and returns a floating point number.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>;
</pre>

<h3>Returns:</h3>

<ul>
<li>(<em>number</em>) The float.</li>
<li>(<em>NaN</em>) If the string is not numeric, returns NaN.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript">    <span class="st0">'95.25%'</span>.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 95.25</span>
    <span class="st0">'10.848'</span>.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 10.848</span>
</pre>

<h3>See Also:</h3>

<ul>
<li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseFloat">MDC parseFloat</a></li>
</ul>
<h2 id="String:hexToRgb"><a href="#String:hexToRgb">String method: hexToRgb</a></h2>

<p class="description">Converts a hexadecimal color value to RGB. Input string must be in one of the following hexadecimal color formats (with or without the hash).
'#ffffff', #fff', 'ffffff', or 'fff'</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">[</span>array<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>array - (<em>boolean</em>, optional) If true is passed, will output an array (e.g. [255, 51, 0]) instead of a string (e.g. 'rgb(255, 51, 0)').</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) A string representing the color in RGB.</li>
<li>(<em>array</em>) If the array flag is set, an array will be returned instead.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'#123'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'rgb(17, 34, 51)'</span>
<span class="st0">'112233'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'rgb(17, 34, 51)'</span>
<span class="st0">'#112233'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// returns [17, 34, 51]</span>
</pre>

<h2 id="String:rgbToHex"><a href="#String:rgbToHex">String method: rgbToHex</a></h2>

<p class="description">Converts an RGB color value to hexadecimal. Input string must be in one of the following RGB color formats.
'rgb(255, 255, 255)', or 'rgba(255, 255, 255, 1)'</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">[</span>array<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>array - (<em>boolean</em>, optional) If true is passed, will output an array (e.g. ['ff', '33', '00']) instead of a string (e.g. '#ff3300').</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.</li>
<li>(<em>array</em>) If the array flag is set, an array will be returned instead.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="st0">'rgb(17, 34, 51)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns '#112233'</span>
<span class="st0">'rgb(17, 34, 51)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// returns ['11', '22', '33']</span>
<span class="st0">'rgba(17, 34, 51, 0)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'transparent'</span>
</pre>

<h3>See Also:</h3>

<ul>
<li><a href="Array.html#Array:rgbToHex">Array:rgbToHex</a></li>
</ul>
<h2 id="String:substitute"><a href="#String:substitute">String method: substitute</a></h2>

<p class="description">Substitutes keywords in a string using an object/array.
Removes undefined keywords and ignores escaped keywords.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">substitute</span><span class="br0">(</span>object<span class="br0">[</span>, regexp<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>object - (<em>mixed</em>) The key/value pairs used to substitute a string.</li>
<li>regexp - (<em>regexp</em>, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) - The substituted string.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="kw2">var</span> myString = <span class="st0">'{subject} is {property_1} and {property_2}.'</span>;
<span class="kw2">var</span> myObject = <span class="br0">{</span>subject: <span class="st0">'Jack Bauer'</span>, property_1: <span class="st0">'our lord'</span>, property_2: <span class="st0">'saviour'</span><span class="br0">}</span>;
myString.<span class="me1">substitute</span><span class="br0">(</span>myObject<span class="br0">)</span>; <span class="co1">// returns Jack Bauer is our lord and saviour</span>
</pre>

<h2 id="String:stripScripts"><a href="#String:stripScripts">String method: stripScripts</a></h2>

<p class="description">Strips the String of its <em><script></script></em> tags and anything in between them.</p>

<h3>Syntax:</h3>

<pre class="javascript">myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="br0">[</span>evaluate<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>evaluate - (<em>boolean</em>, optional) If true is passed, the scripts within the String will be evaluated.</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>string</em>) - The String without the stripped scripts.</li>
</ul>
<h3>Examples:</h3>

<pre class="javascript"><span class="kw2">var</span> myString = <span class="st0">"&lt;script&gt;alert('Hello')&lt;/script&gt;Hello, World."</span>;
myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// returns 'Hello, World.'</span>
myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// alerts 'Hello', then returns 'Hello, World.'</span>
</pre>
    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>
